<template>
  <a-modal
    :open="open"
    width="640px"
    :title="rowData?.name ? '修改医技分类' : '新增医技分类'"
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    centered
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="分类编号" class="w-1/2" v-bind="validateInfos.categoryCode">
        <a-input v-model:value="formState.categoryCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="排序号" class="w-1/2">
        <a-input v-model:value="formState.outorder" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="分类名称" class="w-full" v-bind="validateInfos.categoryName">
        <a-input v-model:value="formState.categoryName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="前缀字母" class="w-1/2">
        <a-input v-model:value="formState.prefixLetter" placeholder="请输入" />
      </a-form-item>
      <a-form-item label=" " class="w-1/2" />
      <a-form-item label="医技主任" class="w-1/2">
        <SelectTable
          v-model:value="formState.categoryEmpName"
          type="医生"
          @select=";(formState.categoryEmpId = $event.empId), (formState.categoryEmpName = $event.empName)"
        />
      </a-form-item>
      <a-form-item label="补零位数" class="w-1/2">
        <a-input v-model:value="formState.zeroFill" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="PACS关联" class="w-full">
        <a-input v-model:value="formState.pacsCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="科室" class="w-full">
        <SelectSection v-model:value="formState.deptCode" multiple />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'ok'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  rowData: {
    type: Object,
    default: () => ({})
  }
})
const formState = ref({})
const rulesState = ref({
  categoryCode: [{ required: true, message: '请选择分类编号' }],
  categoryName: [{ required: true, message: '请选择分类名称' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)

watchEffect(() => {
  if (props.open) {
    formState.value = { ...props.rowData }
  }
})
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/ris/dictCategory/doSaveDictCategory', {
    ...formState.value,
    deptCode: formState.value.deptCode?.join(',')
  })
  if (code !== 0) return message.error(msg || '保存失败')
  message.success('保存成功')
  onCancel()
  emit('ok')
}
</script>
<style lang="less" scoped></style>
